package com.imooc.ecommerce.config;

import feign.Request;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Logger;

import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * open feign 配置类
 * @Author: zhangshao
 * @Since: 2023/11/29 17:18
 */
@Configuration
public class FeignConfig {

	/**
	 * 开启 feign日志
	 * @return
	 */
	@Bean
	public Logger.Level feignLogger(){
		// 需要注意,日志级别需要修改成 debug
		return Logger.Level.FULL;
	}

	/**
	 * period =100 发起请求的时间间隔，单位是ms
	 * maxPeriod = 1000,发起当前请求的最大时间间隔,单位是毫秒
	 * maxAttempts = 5 最多请求次数
	 *
	 * openFeign 开启重试机制
	 * @return
	 */
	public Retryer feignRetryer(){
		return new Retryer.Default(
			 100,
			SECONDS.toMillis(1),
			5
		);
	}

	public static final int CONNECT_TIMEOUT_MILLS = 5000;
	public static final int READ_TIMEOUT_MILLS= 5000;

	/**
	 * 对请求的连接和响应时间进行限制
	 * @return
	 */
	@Bean
	public Request.Options options(){
		return new Request.Options(
				CONNECT_TIMEOUT_MILLS, TimeUnit.MICROSECONDS,
				READ_TIMEOUT_MILLS,TimeUnit.MICROSECONDS,
				true
		);
	}

}
